package com.uni.service.impl;

import com.uni.dao.*;
import com.uni.pojo.*;
import com.uni.service.RoleService;
import com.uni.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Author:      Unirithe/Mr.Chao
 * CSDN:        https://blog.csdn.net/Unirithe
 * CreatedTime:  2022/2/5 9:05
 **/
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private RolePermsMapper rolePermsMapper;
    @Autowired
    private PermMapper permMapper;
    @Override
    public Role queryById(Integer id) {
        return roleMapper.selectByPrimaryKey(id);
    }

    @Override
    public User setRole(User user, Integer roleId) {
        return userRoleMapper.insert(new UserRole(user.getId(), roleId)) == 1 ? user : null;
    }

    @Override
    public Role queryRoleByUserCode(User userInfo) {
        // 检查用户是否存在
        User user = userService.getUserByCode(userInfo.getCode());
        if(user == null) return null;
        // 若存在则获取用户角色信息
        // (1) 获取用户-角色中间表信息
        UserRole userRole = userRoleMapper.selectByExample(new UserRoleExample() {{
            createCriteria().andUserIdEqualTo(user.getId());
        }}).get(0);
        // (2) 根据中间表查找角色具体的信息
        return roleMapper.selectByPrimaryKey(userRole.getRoleId());
    }

    @Override
    public List<Role> queryAllRole() {
        return roleMapper.selectAllWithPerm();
    }

    @Override
    public Role updateRoleByCode(User user, Role role) {
        User user1 = userMapper.selectByExampleWithBLOBs(new UserExample() {{
            createCriteria().andCodeEqualTo(user.getCode());
        }}).get(0);
        // 查询角色ID
        UserRole userRole = userRoleMapper.selectByExample(new UserRoleExample() {{
            createCriteria().andUserIdEqualTo(user1.getId());
        }}).get(0);

        // 修改中间表的角色ID
        userRole.setRoleId(role.getId());
        int i = userRoleMapper.updateByPrimaryKeySelective(userRole);
        // 修改用户表中里的角色ID
        return i == 1 ? role : null;
    }

    @Override
    public List<Role> queryAllROleWithPerm() {
        return roleMapper.selectAllWithPerm();
    }

    @Override
    public int addPerm(Integer roleId, Integer permId) {
        return rolePermsMapper.insert(new RolePerms(roleId, permId));
    }

    @Override
    public int removePermByRole(Integer roleId, Integer permId) {
        return rolePermsMapper.deleteSelective(new RolePerms(roleId, permId));
    }

    @Override
    public List<Perm> queryAllPerms() {
        return permMapper.selectByExample(new PermExample());
    }

    @Override
    public List<Perm> queryPermsBySome(Map<String, String> map) {
        return permMapper.selectBySome(map);
    }

}
